package cn.lijiajia3515.cairo.auth.starter.autoconfigure;

import cn.lijiajia3515.cairo.auth.CairoAuthProperties;
import cn.lijiajia3515.cairo.starter.feign.autoconfigure.CairoFeignOAuth2ClientAutoConfiguration;
import cn.lijiajia3515.cairo.starter.feign.properties.CairoFeignOAuth2ClientProperties;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(CairoFeignOAuth2ClientAutoConfiguration.class)
@AutoConfigureAfter(CairoFeignOAuth2ClientAutoConfiguration.class)
@ConditionalOnBean(CairoAuthProperties.class)
@ConditionalOnProperty(value = "cairo.feign.oauth2.access-token-url", matchIfMissing = true)
public class CairoAuthFeignAutoConfiguration {

	public CairoAuthFeignAutoConfiguration(CairoAuthProperties cairoAuth, CairoFeignOAuth2ClientProperties feignOAuth2Client) {
		if (cairoAuth.getAccessTokenUrl() != null && !cairoAuth.getAccessTokenUrl().isBlank() && feignOAuth2Client.getAccessTokenUrl() == null) {
			feignOAuth2Client.setAccessTokenUrl(cairoAuth.getAccessTokenUrl());
		}
	}


}
